local jingyan = fk.CreateSkill {
  name = "3s__jingyan",
}

Fk:loadTranslationTable {
  ["3s__jingyan"] = "獍焰",
  [":3s__jingyan"] = "当你成为【杀】的目标时，若你的装备区内的牌为场上最多，你可发动<a href=':tianming'>【天命】</a>；"..
  "然后若你的装备区内的牌不为场上最多，你须废除一个空装备栏；一名角色的回合结束后，若你的装备栏均废除，你用所有牌发动【烛鼎】，然后你死亡。",
  ["tianming"] = "天命",
  [":tianming"] = "当你成为【杀】的目标时，你可以弃置两张牌（不足则全弃，无牌则不弃），然后摸两张牌；然后若场上体力唯一最多的角色不为你，"..
  "该角色也可以如此做。",
  ["#tianming-invoke"] = "天命：你可以弃置两张牌（不足则全弃，无牌则不弃），然后摸两张牌",
  ["#jingyan-choice"] = "獍焰：选择废除的装备栏",
  ["#final_zhuding-ask"] = "獍焰：你须用所有牌发动【烛鼎】",
}

jingyan:addEffect(fk.TargetConfirmed, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(jingyan.name) and data.card.trueName == "slash"
    and table.every(player.room:getAlivePlayers(), function(p)
      return #p:getCardIds("e") <= #player:getCardIds("e")
    end)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local ids = table.filter(player:getCardIds("he"), function(id)
      return not player:prohibitDiscard(id)
    end)
    if #ids <= 2 then
      if room:askToSkillInvoke(player, {
        skill_name = "tianming",
        prompt = "#tianming-invoke"
      }) then
        event:setCostData(self, {cards = ids})
        return true
      end
    else
      local cards = room:askToDiscard(player, {
        min_num = 2,
        max_num = 2,
        include_equip = true,
        skill_name = "tianming",
        cancelable = true,
        prompt = "#tianming-invoke",
        skip = true,
      })
      if #cards > 0 then
        event:setCostData(self, {cards = cards})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if #event:getCostData(self).cards > 0 then
      room:throwCard(event:getCostData(self).cards, jingyan.name, player, player)
    end
    if not player.dead then
      player:drawCards(2, jingyan.name)
    end
    if not table.every(player.room:getAlivePlayers(), function(p)
        return #p:getCardIds("e") <= #player:getCardIds("e") end) then
      local all_choices = {"WeaponSlot", "ArmorSlot", "DefensiveRideSlot", "OffensiveRideSlot", "TreasureSlot"}
      local subtypes = {Card.SubtypeWeapon, Card.SubtypeArmor, Card.SubtypeDefensiveRide, Card.SubtypeOffensiveRide, Card.SubtypeTreasure}
      local choices = {}
      for i = 1, 5, 1 do
        if #player:getAvailableEquipSlots(subtypes[i]) > 0 then
        table.insert(choices, all_choices[i])
        end
      end
      if #choices == 0 then return end
      local choice = player.room:askToChoice(player, {
        choices = choices,
        skill_name = jingyan.name,
        prompt = "#jingyan-choice",
        all_choices = all_choices,
      })
      room:abortPlayerArea(player, {choice})
    end
    local to = table.filter(room.alive_players, function (p)
      return table.every(room.alive_players, function (q)
        return p.hp >= q.hp
      end)
    end)
    if #to ~= 1 or to[1] == player then return end
    to = to[1]
    local ids = table.filter(to:getCardIds("he"), function(id)
      return not to:prohibitDiscard(id)
    end)
    if #ids <= 2 then
      if room:askToSkillInvoke(to, {
        skill_name = "tianming",
        prompt = "#tianming-invoke"
      }) then
        if #ids > 0 then
          room:throwCard(ids, jingyan.name, to, to)
          if not to.dead then
            to:drawCards(2, jingyan.name)
          end
        end
      end
    else
      if #room:askToDiscard(to, {
        min_num = 2,
        max_num = 2,
        include_equip = true,
        skill_name = "tianming",
        cancelable = true,
        prompt = "#tianming-invoke",
      }) == 2 and
        not to.dead then
        to:drawCards(2, jingyan.name)
      end
    end
  end,
})

jingyan:addEffect(fk.TurnEnd, {
  is_delay_effect = true,
  anim_type = "nagative",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(jingyan.name) and #player:getAvailableEquipSlots() == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_names = {}
    for _, id in ipairs(Fk:getAllCardIds()) do
      local card = Fk:getCardById(id)
      if card:isCommonTrick() and card.is_damage_card and not card.is_derived then
        table.insertIfNeed(all_names, card.name)
      end
    end
    room:askToUseVirtualCard(player, {
      name = all_names,
      skill_name = "3s__zhuding",
      prompt = "#final_zhuding-ask:::",
      subcards = player:getCardIds("he"),
      cancelable = false,
    })
    room:killPlayer{
      who = player,
      killer = player,
    }
  end
})
        
  

return jingyan